<?php
namespace backend\modules\member\models;

use common\libs\Constants;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\activeRecords\TUser;

/**
 * MemberSearch represents the model behind the search form of `app\models\activeRecords\TUser`.
 */
class MemberSearch extends TUser
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'status', 'register_time', 'last_login_time', 'reg_game_id', 'reg_server_id', 'is_deducted'], 'integer'],
            [['username', 'auth_key', 'password_hash', 'password_reset_token', 'avatar', 'email', 'join_ip', 'spread_key_1', 'spread_key_2', 'reg_game_name', 'reg_server_name', 'spread_key'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = TUser::find();

        // add conditions that should always apply here
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'status' => $this->status,
            'last_login_time' => $this->last_login_time,
            'reg_game_id' => $this->reg_game_id,
            'reg_server_id' => $this->reg_server_id,
            'is_deducted' => $this->is_deducted,
        ]);

        if(!empty($this->register_time)){
            $time = explode(' - ',$this->register_time);
            $query->andFilterWhere(['between','register_time',strtotime($time[0]),strtotime($time[1])+Constants::ONE_DAY-1]);
        }

        $query->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'auth_key', $this->auth_key])
            ->andFilterWhere(['like', 'password_hash', $this->password_hash])
            ->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])
            ->andFilterWhere(['like', 'avatar', $this->avatar])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'join_ip', $this->join_ip])
            ->andFilterWhere(['like', 'spread_key_1', Constants::ANCHOR])
            ->andFilterWhere(['like', 'reg_game_name', $this->reg_game_name])
            ->andFilterWhere(['like', 'reg_server_name', $this->reg_server_name])
            ->andWhere('length(spread_key)>0');
        $auth = Yii::$app->authManager;
        $roleGuildManager = Constants::ROLE_GUILD_MANAGER;
        $roleGuildChannel = Constants::ROLE_GUILD_CHANNEL;
        if(!empty($auth->getRolesByUser(Yii::$app->user->getId())[$roleGuildManager])){
            $query->andFilterWhere(['like', 'spread_key', Yii::$app->user->getIdentity()->parent]);
        }elseif(!empty($auth->getRolesByUser(Yii::$app->user->getId())[$roleGuildChannel])){
            $query->andFilterWhere(['like', 'spread_key_2', Yii::$app->user->getIdentity()->position]);
        }else{
            $query->andFilterWhere(['like', 'spread_key', $this->spread_key]);
            $query->andFilterWhere(['like', 'spread_key_2', $this->spread_key_2]);
        }

        return $dataProvider;
    }
}
